package net.a6polo.a6poloweibo;

import java.io.IOException;

import net.a6polo.service.Service;
import net.a6polo.service.SnsType;
import net.a6polo.service.domain.Account;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.CommentsAPI;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import android.widget.Toast;

public class CommentWeiboactivity extends Activity implements OnClickListener {

	private Button sendButton;
	private EditText contentTextView;
	private StatusesAPI statuesAPI;
	private CommentsAPI commentsAPI;
	private String weiboId;
	private boolean isComment = true;;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_weibo);

		Service service = new Service(CommentWeiboactivity.this);
		Account sinaAccount = service.getLastAccountByType(SnsType.SINA_WEIBO);
		Oauth2AccessToken token = new Oauth2AccessToken(sinaAccount.getAccessToken(), sinaAccount.getExpiresIn());

		statuesAPI = new StatusesAPI(token);
		commentsAPI = new CommentsAPI(token);

		sendButton = (Button) findViewById(R.id.menu_send);
		sendButton.setOnClickListener(this);

		contentTextView = (EditText) findViewById(R.id.status_new_content);

		Intent intent = getIntent();
		weiboId = intent.getStringExtra("sourceId");
		String type = intent.getStringExtra("type");
		
		if("repost".equals(type)) {
			isComment = false;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menu_send: {
			send();
			break;
		}

		default:
			break;
		}

	}

	public void send() {
		
		if(isComment) {
			commentsAPI.create(contentTextView.getText().toString(), Long.parseLong(weiboId), false, new RepostRequestListener());
		} else {
			statuesAPI.repost(Long.parseLong(weiboId), contentTextView.getText().toString(), WeiboAPI.COMMENTS_TYPE.NONE,
					new RepostRequestListener());
		}
		

		finish();
	}

	class RepostRequestListener implements RequestListener {

		@Override
		public void onComplete(String arg0) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(CommentWeiboactivity.this, R.string.weibosdk_send_sucess, Toast.LENGTH_LONG).show();

				}
			});

		}

		@Override
		public void onError(WeiboException e) {
			final WeiboException e1 = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(
							CommentWeiboactivity.this,
							String.format(CommentWeiboactivity.this.getString(R.string.weibosdk_send_failed) + ":%s",
									e1.getMessage()), Toast.LENGTH_LONG).show();
				}
			});
		}

		@Override
		public void onIOException(IOException e) {
			final Exception e1 = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(
							CommentWeiboactivity.this,
							String.format(CommentWeiboactivity.this.getString(R.string.weibosdk_send_failed) + ":%s",
									e1.getMessage()), Toast.LENGTH_LONG).show();
				}
			});
		}

	}

}
